<?php
require_once("conf/conf.inc.php");
require_once("lib/util.lib.php");
require_once("class/Formulaire.class.php");

if (!isset($_GET["evenement"]))
  die("");
  
$nom_referent = $_GET["evenement"];
if (!isset($_SESSION["userid"]))
  $formulaire = new Formulaire($nom_referent,true);
else
  $formulaire = new Formulaire($nom_referent,false);

if (!empty($_POST)){
  if($formulaire->enregistrerFormulaire($_POST))
    die ("Vos informations ont &eacute;t&eacute; sauvegard&eacute;. Merci!");
}
?>

  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link type="text/css" rel="stylesheet" href="style/ui.datepicker.css" />
    <link type="text/css" rel="stylesheet" href="style/skins/gray/skin.css" />
    
    <script type="text/javascript" src="lib/jquery-1.2.3.min.js"></script>
    <script type="text/javascript" src="lib/ui.datepicker.js"></script>
    <script type="text/javascript" src="lib/ui.datepicker-fr.js"></script>
  
  </head>
<form method="post">

<table class="formulaire_evenement">
<?php
  echo "<tr><th colspan='2'>$formulaire->titre_evenement</th></tr>";
  if ($formulaire->disponible){
    $champs = $formulaire->getChamps();
    foreach($champs as $champ){
      echo "<tr valign='top'><td>{$champ["etiquette"]}</td><td>";
      switch($champ["type"]){
        case 1:
          echo "<input type='text' name='formulaire_champ_{$champ["id"]}' value='' />";
        break;
        case 2:
          echo "<textarea name='formulaire_champ_{$champ["id"]}' rows='6'></textarea>";
        break;
        case 3:
          $membres = $formulaire->getMembres();
          echo "<select name='formulaire_champ_{$champ["id"]}[]".($champ["multiple"]? " multiple='multiple' size='5'": "") ."'>
              <option value=\"\"></option>";
          foreach($membres as $membre){
            echo "<option value=\"{$membre["id"]}\">{$membre["nom"]}, {$membre["prenom"]}</option>";
          }
          echo "</select>";
        break;
        case 4:
          echo '<input type="text" id="'.$champ["id"].'" readonly="readonly" size="25" name="formulaire_champ_'.$champ["id"].'" title="S&eacute;lectionnez une date. Vous pouvez choisir une plage en cliquant 2 fois dans le calendrier" />
            <script type="text/javascript">';
          echo "
              jQuery(function($){
                $('#".$champ['id']."').datepicker({";
          if ($champ["multiple"]){
          echo "            rangeSelect: true, 
                            rangeSeparator: ' au ',"; 
          }
          echo "            numberOfMonths: 2,
                            prevText: '<img src=\"style/btn_previous.gif\" />',
                            nextText: '<img src=\"style/btn_next.gif\" />',
                            changeMonth: false, changeYear: false });
              });
            </script>";
        break;
        case 5:
          echo '<input type="text" id="'.$champ["id"].'" readonly="readonly" size="25" name="formulaire_champ_'.$champ["id"].'" title="S&eacute;lectionnez une date. Vous pouvez choisir une plage en cliquant 2 fois dans le calendrier"  />
            <script type="text/javascript">
              debut = "'.convertToDate($formulaire->debut_evenenement).'".split("/");
              fin = "'.convertToDate($formulaire->fin_evenenement).'".split("/");';
          echo "
              jQuery(function($){
                $('#".$champ['id']."').datepicker({
                                    minDate: new Date(debut[2], debut[1], debut[0]), 
                                    maxDate: new Date(fin[2], fin[1], fin[0]),";
          if ($champ["multiple"]){
          echo "                    rangeSelect: true, 
                                    rangeSeparator: ' au ',"; 
          }
          echo "                    numberOfMonths: 2,
                                    prevText: '<img src=\"style/btn_previous.gif\" />',
                                    nextText: '<img src=\"style/btn_next.gif\" />',
                                    changeMonth: false, changeYear: false });
              });
            </script>";
        break;
        case 6:
          echo "<select name='formulaire_champ_{$champ["id"]}'>";
          $options = explode("\r\n",$champ["selection"]);
          foreach($options as $option){
            echo "<option value=\"$option\">$option</option>";
          }
          echo "</select>";
        break;
        case 7:
          echo "<select name='formulaire_champ_{$champ["id"]}[]'".($champ["multiple"]? " multiple='multiple' size='5'": "size='1'").">";
          $options = explode("\r\n",$champ["selection"]);
          foreach($options as $option){
            echo "<option value=\"$option\">$option</option>";
          }
          echo "</select>";
        break;
        case 8:
          $type = ($champ["multiple"]? "checkbox": "radio");
          $options = explode("\r\n",$champ["selection"]);
          foreach($options as $option){
            echo "<input type='$type' name=\"formulaire_champ_{$champ["id"]}[]\" value=\"$option\">$option<br/>";
          }
        break;
      }//switch
      echo "</td></tr>";
    }//for each
    echo '</table>
 <input type="submit" value="Enregistrer" />';
  }
 ?>
</form>
